package com.sciener.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import com.sciener.entity.Car;
import com.sciener.entity.Licence;
import com.sciener.service.CarService;
import com.sciener.service.LicenceService;

public class licenceServlet extends HttpServlet {
	private static LicenceService licenceService = new LicenceService();

	public void destroy() {
		super.destroy(); 
	}

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
		
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String action = request.getParameter("action");
		if(action.equals("getAllLicenceInfo"))
		{
			List<Licence> licences = licenceService.getAllLicenceInfo();
			out.println(JSONArray.fromObject(licences));
		}
		/*else if(action.equals("login"))
		{
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			boolean iscucess = userservice.rightUserAndPwd(username,password);
			out.print(iscucess);
		}*/
		else if(action.equals("getlicenceinfobyuserid"))
		{
			String userid = request.getParameter("userid");
			Licence l = licenceService.getLicenceInfoByuserid(Integer.valueOf(userid));
			if(l!=null)
			{
				out.println(JSONArray.fromObject(l));
			}
			else
			{
				out.print("nolicence");
			}
		}
		
		out.close();
	}

	
	public void init() throws ServletException {
		
	}
}
